<template>
  <el-dialog
    ref="EditAgent"
    title="选择代理"
    width="300px"
    :close-on-click-modal="false"
    :visible.sync="params.visible"
  >
    <compTree
      ref="tree"
      :show-search="false"
      :tree-show-checkbox="showCheckbox"
      :tree-default-expanded-keys="treeDefaultExpandedKeys"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
      :tree-data="treeData"
      :comp-tree-height="350"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
/**
 * 入参：params:{
 *          id
 *		      isMulti
 *			      isAllowNull 是否允许选空值
 *      }
 * 出参：{ agentId, agentIP, agentName, agentIds, agentIPs }
 */
import { SQLTable } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
import { getAgentList } from '@/api/core'
import DialogBtn from './DialogBtn.vue'
export default {
  name: 'SelectAgentLayer',
  components: { DialogBtn },
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false, // 弹窗是否可见
          id: '' // 默认选中的节点
        }
      }
    }
  },
  data() {
    return {
      visible: false,
      treeData: [],
      data: {},
      treeDefaultExpandedKeys: [],
      treeDefaultCheckedKeys: [],
      showCheckbox: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.data = this.params
      this.data.id = this.data.id || ''
      this.data.isMulti = this.data.isMulti || false
      this.showCheckbox = this.data.isMulti
      this.data.isAllowNull =
        this.data.isAllowNull === undefined ? true : this.data.isAllowNull
      getAgentList().then((vPar) => {
        const nodesTop = []
        const nodeMap = new Map()
        let table = new SQLTable(vPar.getTable('k_agent_group'))
        table.first()
        while (!table.eof()) {
          const node = {}
          node.id = table.getValueByName('ID')
          node.pid = table.getValueByName('PID')
          node.name = table.getValueByName('Name')
          node.text = node.name
          // node.icon = 'folder'
          node.folder = true
          nodeMap.set(node.id, node)
          const pNode = Global.getTreeviewPNode(nodeMap, nodesTop, node.pid)
          pNode.push(node)
          table.next()
        }
        table = new SQLTable(vPar.getTable('k_agent'))
        table.first()
        while (!table.eof()) {
          const node = {}
          node.id = table.getValueByName('ID')
          node.pid = table.getValueByName('PID')
          node.ip = table.getValueByName('IP')
          node.name = table.getValueByName('Name')
          node.system = table.getValueByName('System')
          node.text = Global.strFormat('{0}[{1}]', node.ip, node.name)
          node.icon = Global.getSystemIconStr(
            table.getValueByName('System').trim()
          )
          node.online = table.getValueByName('Online') === '1'
          if (node.id === this.data.id) {
            node.state = {
              selected: true,
              checked: true
            }
            this.treeDefaultExpandedKeys.push(node.pid)
            this.treeDefaultCheckedKeys.push(node.id)
            this.$refs.tree.setSelect(node.id)
          }
          nodeMap.set(node.id, node)
          const pNode = Global.getTreeviewPNode(nodeMap, nodesTop, node.pid)
          pNode.push(node)
          table.next()
        }
        this.treeData = nodesTop
      })
    },
    confirm() {
      let agentId = ''
      let agentIP = ''
      let agentName = ''
      let agentIds = ''
      let agentIPs = ''
      if (this.data.isMulti) {
        const nodes = this.$refs.tree.getCheckedNodes()
        const ids = []
        const ips = []
        for (let i = 0; i < nodes.length; i++) {
          if (!nodes[i].folder) {
            ids.push(nodes[i].id)
            ips.push(nodes[i].ip)
          }
        }
        if (!this.data.isAllowNull && ids.length === 0) { return this.$message.warning('请选择机器。') }
        agentIds = ids.join(',')
        agentIPs = ips.join(',')
      } else {
        const node = this.$refs.tree.getCurrentNode()
        if (node === null || node.folder) {
          if (!this.data.isAllowNull) { return this.$message.warning('请选择机器。') }
          agentId = ''
          agentName = ''
          agentIP = ''
        } else {
          agentId = node.id
          agentName = node.name
          agentIP = node.ip
        }
      }
      const params = { agentId, agentIP, agentName, agentIds, agentIPs }
      if (this.params.confirm !== undefined) {
        this.params.confirm(params)
      } else {
        this.$emit('confirm', params)
      }
    }
  }
}
</script>

<style scoped>
</style>
